﻿using Admin2024.Api.Filter;
using Admin2024.Application.Contracts.Role;
using Admin2024.Application.Contracts.Role.Dto;
using Admin2024.Application.Role;
using Admin2024.Domain.Entity.System;
using Admin2024.Domain.ObjValue;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Admin2024.Api.Controller
{
    [ApiController]
    //[TypeFilter(typeof(CtmAuthorizationFilterAttribute))]

    public class AppRoleController : ControllerBase
    {
        private readonly IRoleInfoService roleInfoService;
        private readonly IRolePermissionAssignService rolePermissionAssignService;
       private  readonly IRoleStateService roleStateService;
        public AppRoleController(IRoleInfoService roleInfoService, IRolePermissionAssignService rolePermissionAssignService, IRoleStateService roleStateService)
        {
            this.roleInfoService = roleInfoService;
            this.rolePermissionAssignService = rolePermissionAssignService;
            this.roleStateService = roleStateService;
        }


        //获取指定角色
        [HttpGet("/api/RoleGetById/{Id}")]
        public async Task<DomainResult<RoleDto>> GetRoleById(Guid Id)
        {
            return await roleInfoService.GetRoleById(Id);
        }
        //获取所有角色（分页）
        [HttpGet("/api/GetRoleAllPage")]
        public async Task<DomainResult<List<RoleDto>>> GetRoleAll(int page = 1, int pageSize = 5)
        {
            return await roleInfoService.GetRolesWithPagination(page, pageSize);
        }
        //删除角色
        [HttpDelete("/api/DeleteRole/{Id}")]
        public async Task<DomainResult<RoleDto>> DeleteRole(Guid Id)
        {
            return await roleInfoService.RemoveRole(Id);
        }
        //软删除角色
        [HttpPut("/api/IsDeleteRole/{Id}")]
        public async Task<DomainResult<bool>> IsDeleteRole(Guid Id)
        {
            return await roleStateService.DisableRole(Id);
        }

        //禁用启用
        [HttpPut("/api/EnableRole/{Id}")]
        public async Task<DomainResult<bool>> EnableRole(Guid Id)
        {
            return await roleStateService.EnableRole(Id);
        }
        //新增角色
        [HttpPost("/api/AddRole")]
        public async Task<DomainResult<RoleDto>> AddRole(RoleCreateDto roleCreateDto)
        {
            return await roleInfoService.CreateRole(roleCreateDto.RoleName, roleCreateDto.Description);
        }
        //修改角色
        [HttpPut("/api/UpdateRole/{Id}")]
        public async Task<DomainResult<RoleDto>> UpdateRole(Guid Id, RoleUpdateDto roleUpdate)
        {
            return await roleInfoService.UpdateRoleInfo(Id, roleUpdate);
        }
        //给角色分配权限
        [HttpPost("/api/AssingRoleToPer")]
        public async Task<DomainResult<string>> AssingRoleToPer(Guid roleId,Guid PerId)
        {
            return await rolePermissionAssignService.CreatePermission(roleId, PerId);
        }

        //给角色分配权限
        [HttpDelete("/api/DeleteRoleToPer")]
        public async Task<DomainResult<string>> DeleteRoleToPer(Guid roleId, Guid PerId)
        {
            return await rolePermissionAssignService.DeletePermission(roleId, PerId);
        }

        //获取角色的所有权限
        //获取角色的所有权限
        [HttpGet("/api/GetRoleAllPer/{Id}")]
        public async Task<DomainResult<List<AppPermission>>> GetRoleAllPer(Guid Id)
        {
            return await rolePermissionAssignService.GetListRolePermission(Id);
        }
    }
}
